package com.xiaoq.sites.endpoint;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoq.apis.content.ContentInfo4Vo;
import com.xiaoq.apis.content.IContentApi;
import com.xiaoq.apis.user.IMerchantMgrApi;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.util.MathUtil;
import com.xiaoq.hotel.Vo.HotelVo;
import com.xiaoq.hotel.endpoint.HotelEndpoint;
import com.xiaoq.hotel.entity.HotelEntity;
import com.xiaoq.hotel.service.IHotelService;
import com.xiaoq.route.sites.entity.SitesRouteEntity;
import com.xiaoq.route.sites.service.ISitesRouteService2;
import com.xiaoq.route.sites.vo.SitesRouteVo;
import com.xiaoq.sites.entity.BannerEntity;
import com.xiaoq.sites.entity.SitesEntity;
import com.xiaoq.sites.service.IBannerService;
import com.xiaoq.sites.service.ISitesService;
import com.xiaoq.sites.vo.BannerVo;
import com.xiaoq.sites.vo.QiChaVo;
import com.xiaoq.sites.vo.SitesDetailVo;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api(tags={"七叉镇聚合"})
@RestController
@RequestMapping("/api/v1/qicha")
public class QiChaEndpoint {

    @Autowired
    private IHotelService hotelSer;

    @Autowired
    private ISitesRouteService2 iSitesRouteService;

    @Autowired
    private IBannerService bannerSer;

    @Autowired
    private ISitesService sitesService;


    public class QiChaDataResponse extends GenericDataResponse<QiChaVo> {}

    @Reference(interfaceClass= IContentApi.class,check=false,version="1.0.0")
    private IContentApi contentApi;

    @ApiOperation(value = "首页聚合接口", notes = "首页聚合接口")
    @ApiResponses({ @ApiResponse(code = 200, response = QiChaDataResponse.class, message = "首页聚合接口") })
    @GetMapping(value = "/shouye", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> findHotels(
            @RequestParam(value = "cityCode") @ApiParam(value = "城市编码") String cityCode,
            @RequestParam(value="localLongitude",required=false)@ApiParam(value="当前经度",required=false)Double localLongitude,
            @RequestParam(value="localLatitude",required=false)@ApiParam(value="当前纬度",required=false)Double localLatitude,
            HttpServletRequest request) {


        // 获取 Banner
        List<BannerEntity> bannerEntityList = bannerSer.findBannerList("1000",cityCode);
        List<BannerVo> bannerVoList = new ArrayList<>();
        BannerVo bannerVo = null;
        for(BannerEntity banner: bannerEntityList){
            bannerVo = new BannerVo();
            BeanUtils.copyProperties(banner, bannerVo);
            bannerVoList.add(bannerVo);
        }



        // 获取景区
        List<SitesEntity> sitesEntityList;
        sitesEntityList = sitesService.findSitesListbyZoneCity(cityCode,null);
        if(sitesEntityList!=null&&sitesEntityList.size()>6){
            sitesEntityList = sitesEntityList.subList(0,6);
        }
        List<SitesDetailVo> sitesDetailVos = new ArrayList<>();
        SitesDetailVo sitesDetailVo = null;
        for(SitesEntity sites:sitesEntityList){
            sitesDetailVo = new SitesDetailVo();
            BeanUtils.copyProperties(sites,sitesDetailVo);
            if (localLongitude != null && localLatitude != null && localLongitude != 0.00 && localLatitude != 0.00) {
                Double shopLongitude = sitesDetailVo.getCenterLongitude();
                Double shopLatitude = sitesDetailVo.getCenterLatitude();
                Double distance = MathUtil.distance(localLongitude, localLatitude, shopLongitude, shopLatitude);
                sitesDetailVo.setDistance(distance);
                String distanceString = MathUtil.distance(distance);
                sitesDetailVo.setDistanceString(distanceString);
            }
            sitesDetailVos.add(sitesDetailVo);
        }



        // 获取线路
        List<SitesRouteEntity> sitesRoutes =  iSitesRouteService.findSitesRoute(cityCode);
        List<SitesRouteVo> sitesRouteVos = new ArrayList<>();
        SitesRouteVo sitesRouteVo = null;
        for(SitesRouteEntity sites:sitesRoutes){
            sitesRouteVo = new SitesRouteVo();
            BeanUtils.copyProperties(sites,sitesRouteVo);
            sitesRouteVos.add(sitesRouteVo);
        }

        // 获取文章
        List<ContentInfo4Vo> contents = contentApi.findContentListByCity(cityCode);
        List<ContentInfo4Vo> contentss = new ArrayList<>();
        Set set = new HashSet();
        for(ContentInfo4Vo content: contents){
            System.out.println(content.getCatalogCode());
            if(!set.contains(content.getCatalogCode())){
                set.add(content.getCatalogCode());
                contentss.add(content);
            }
        }

        QiChaVo qiChaVo = new QiChaVo();
        qiChaVo.setBannerVoList(bannerVoList);
        qiChaVo.setSitesDetailVos(sitesDetailVos);
        qiChaVo.setSitesRouteVos(sitesRouteVos);
        qiChaVo.setContents(contentss);
        return QiChaDataResponse.okWithData(qiChaVo);

    }
}
